package com.lm.utils;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class LmFileWriteHelper {
    public static void writeContent(String fileName) throws IOException, URISyntaxException {
        // 获取target/classes目录路径
        Path targetPath = Paths.get(
                LmFileWriteHelper.class.getClassLoader().getResource("").toURI()
        ).getParent(); // 上溯到target目录

        // 构建resources子目录路径
        Path resourcesPath = targetPath.resolve("resources");

        // 确保目录存在
        if (!Files.exists(resourcesPath)) {
            Files.createDirectories(resourcesPath);
        }

        // 构建完整文件路径
        Path outputFile = resourcesPath.resolve(fileName);

        try(FileOutputStream fs = new FileOutputStream(outputFile.toFile());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fs, StandardCharsets.UTF_8));
                ){
            writer.write("芷儿凤凰鸽舍");
            writer.newLine();
            writer.write("镇棚鸽:坦克");
        }

        System.out.println("写入完毕! 路径: " + outputFile.toAbsolutePath());
    }
}
